<?php
	// must be logged in
	gatekeeper();
	
	$title = get_input('title');
	$address = get_input('address');
	
	// must have a title and url
	if (!$title || !$address) {
		register_error(elgg_echo('digg:failed'));
		forward($address);
	}
	
	// create digg object if it doesn't exist 
	$digg_object = get_object_by_description($address);
	if (!$digg_object) {
		$digg_object = new ElggObject;
		$digg_object->subtype = "digg";
		$digg_object->owner_guid = get_loggedin_userid();
		$digg_object->title = $title;
		$digg_object->description = $address;
		$digg_object->access_id = ACCESS_PUBLIC;

		if (!$digg_object->save()) {
			register_error(elgg_echo('digg:failed'));
			forward($address);
		}
	}

	// add annotation on digg object 
	$digg_object->annotate("digg", 1, ACCESS_PUBLIC);
	
	// send user to the page for this digg object
	$digg_url = $CONFIG->wwwroot . "pg/digg/view/" . $digg_object->guid; 
	system_message(sprintf(elgg_echo('digg:success'), $title));
	forward($digg_url);
?>